package subjectAndProfessorship;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import practicalWork.Individual;
import practicalWork.PracticalWork;
import practicalWork.ToHandIn;
import regularlyOfStudents.Regularly;
import mail.Mail;
import mail.VerificadorDeEmail;
import degreeAndSyllabus.EnrolledInDegree;
import exception.AlumnoConLicenciaException;
import exception.AlumnoLibreException;
import exception.NoEsNotaValidaException;
import exception.NoSePuedeTomarExamenException;
import test.Test;
import util.MyDate;

/**
 * @author Nati
 */
public class EnrolledInProfessorship implements Observer{
	/* ******************************** Variables ******************************** */
	private List<Integer> notasEnMateria = new LinkedList<Integer>(); //Parcial y TP
	private EnrolledInDegree student; 
	private Professorship catedraInscripta; 
	private List <MyDate> asistencias= new LinkedList<MyDate>(); 
	private List <Mail> buzon = new LinkedList<Mail>(); 
	
	/* ******************************* Constructor ******************************* */
	public EnrolledInProfessorship(List <MyDate> asistencias, EnrolledInDegree student, Professorship catedra) {
		this.student = student;
		this.catedraInscripta = catedra; 
		this.asistencias = asistencias; 
		this.catedraInscripta.addObserver(this);  
		this.catedraInscripta.addObserver(this);
	}

	/* ********************************* Metodos ********************************* */
	/**
	 * @return int
	 * @
	 * Promedio entre todas las notas de los parciales y tps
	 */
	public int calcularNotaFinal (){
		int nota =0; 
		if (! this.getNotasEnMateria().isEmpty()){
			for (int i: this.getNotasEnMateria()){
				nota = nota + i; 
			}
			nota = nota/this.getNotasEnMateria().size(); 
		}
		return nota; 
	}	

	/**
	 * @return int
	 * @
	 * El porcentaje de clases asistidas por el alumno.
	 */
	public float porcentajePresentismo (){
		if (this.catedraInscripta.getCantClases()==0){
			return 0; 
		}else {
			return (this.asistencias.size()*100)/ this.catedraInscripta.getCantClases();			
		}
	}
	
	public void agregarNota (int nota) throws NoEsNotaValidaException{
		if (nota>10){
			this.notasEnMateria.add(nota);
		}else{
			throw new NoEsNotaValidaException("La nota no es valida");
		}
	}
	
	public String nombreAlumno (){
		return student.getEstudiante().getData().getName(); 
	}
	
	public String apellidoAlumno (){
		return student.getEstudiante().getData().getSurname(); 
	}

	public void update(Observable o, Object arg) {
		VerificadorDeEmail filtro = new VerificadorDeEmail();
		if(filtro.esEmail(((Mail)arg).getDirOrig())){
			this.agregarMail(((Mail)arg));
		}
	}
	
	public void agregarMail(Mail mail) {
		this.getBuzon().add(mail);
	}
	
	public boolean estaAusente (){
		return this.porcentajePresentismo()<(75.0); 
	}

	public Regularly miRegularidad (){
		return this.getStudent().getRegularly(); 
	}
	
	/**
	 * @param test
	 * @param catedra
	 */
	public void rendirExamen (Test test, Professorship catedra){
		try {
			this.miRegularidad().rendirExamen(this, test, catedra);
		} catch (AlumnoLibreException e) {
			e.printStackTrace();
		} catch (AlumnoConLicenciaException e) {
			e.printStackTrace();
		} catch (NoSePuedeTomarExamenException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * @param tp, fecha
	 * @
	 * Rinde un trabajo practico individual
	 */
	public void rendirPWIndividual(PracticalWork tp, MyDate fecha) {
		tp.addHandIn(new Individual(fecha, this));
	}

	public void rendirPWGrupal (ToHandIn tpGrupal, PracticalWork pw){
		pw.addHandIn(tpGrupal); 
	}

	/* ******************************** Get & Set ******************************** */
	public List<Integer> getNotasEnMateria() {
		return notasEnMateria;
	}

	public void setNotasEnMateria(List<Integer> notasEnMateria) {
		this.notasEnMateria = notasEnMateria;
	}

	public EnrolledInDegree getStudent() {
		return student;
	}

	public List<Mail> getBuzon() {
		return buzon;
	}

	public void setBuzon(List<Mail> buzon) {
		this.buzon = buzon;
	}

	public void setStudent(EnrolledInDegree student) {
		this.student = student;
	}

	public void setCatedraInscripta(Professorship catedraInscripta) {
		this.catedraInscripta = catedraInscripta;
	}

	public Professorship getCatedraInscripta() {
		return catedraInscripta;
	}

	public void setAsistencias(List <MyDate> asistencias) {
		this.asistencias = asistencias;
	}

	public List <MyDate> getAsistencias() {
		return asistencias;
	}

	public String getMail() {
		return this.getStudent().getEstudiante().getData().getEmail();
	}
}
